package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.guagifts.UserGuaGiftData;
import com.yanqu.road.entity.qixi.UserQixiGiftData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserGuaGiftDaoImpl extends TempDataObjectDao<UserGuaGiftData> {
    public static String getUserGuaGiftDataSql = "select * from `t_u_gua_gift` where `activityId`=? and `userId`=? and `status`=0";
    public List<UserGuaGiftData> getUserGuaGiftData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<UserGuaGiftData> dataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserGuaGiftDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserGuaGiftData data = parse(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }

    public Map<Long, List<UserGuaGiftData>> getNeedSendMailGift(int activityId) {
        String sql = "select * from `t_u_gua_gift` where `activityId`=? and `status`=0";
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, List<UserGuaGiftData>> dataMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserGuaGiftData data = parse(rs);
                List<UserGuaGiftData> list = dataMap.get(data.getUserId());
                if (list == null) {
                    list = new ArrayList<>();
                    dataMap.put(data.getUserId(), list);
                }
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUserGuaGiftDataSql = "update `t_u_gua_gift` set targetUserId=?,giftType=?,goodsId=?,goodsCount=?,giftTime=?,content=?,status=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(UserGuaGiftData userGuaGiftData) {
        String updateSql = "update `t_u_gua_gift` set status=? where activityId=? and userId=? and targetUserId=? and giftTime=?";
        userGuaGiftData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userGuaGiftData.getStatus()));

        para.put(i++, new DbParameter(Types.INTEGER, userGuaGiftData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaGiftData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaGiftData.getTargetUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaGiftData.getGiftTime()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserGuaGiftDataSql = "insert into `t_u_gua_gift`(activityId,userId,targetUserId,giftType,goodsId,goodsCount,giftTime,content,status) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserGuaGiftData userGuaGiftData) {
        userGuaGiftData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userGuaGiftData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaGiftData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaGiftData.getTargetUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userGuaGiftData.getGiftType()));
        para.put(i++, new DbParameter(Types.INTEGER, userGuaGiftData.getGoodsId()));
        para.put(i++, new DbParameter(Types.INTEGER, userGuaGiftData.getGoodsCount()));
        para.put(i++, new DbParameter(Types.BIGINT, userGuaGiftData.getGiftTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, userGuaGiftData.getContent()));
        para.put(i++, new DbParameter(Types.INTEGER, userGuaGiftData.getStatus()));
        if (getDb().executeNoneQuery(insertUserGuaGiftDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserGuaGiftData parseData(ResultSet rs) throws SQLException {
        UserGuaGiftData userGuaGiftData = new UserGuaGiftData();
        userGuaGiftData.setActivityId(rs.getInt("activityId"));
        userGuaGiftData.setUserId(rs.getLong("userId"));
        userGuaGiftData.setTargetUserId(rs.getLong("targetUserId"));
        userGuaGiftData.setGiftType(rs.getInt("giftType"));
        userGuaGiftData.setGoodsId(rs.getInt("goodsId"));
        userGuaGiftData.setGoodsCount(rs.getInt("goodsCount"));
        userGuaGiftData.setGiftTime(rs.getLong("giftTime"));
        userGuaGiftData.setContent(rs.getString("content"));
        userGuaGiftData.setStatus(rs.getInt("status"));

        return userGuaGiftData;
    }
}
